var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var tools = require('./model/loader/tool');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var postsRouter = require('./routes/posts');
var reviewRouter = require('./routes/reviews');
var util = require('./common/util');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/posts', postsRouter);
app.use('/reviews', reviewRouter);

app.post('/upfile', tools.upload.single('imgfile'), function(req, res){
    if (!req.file) {
      return res.status(400).send('未接收到文件');
    }
    res.send(util.FormatJSONData(201, '上传成功', req.file.filename));
  });

module.exports = app;
